const request = require('request');
const config  = require('../config/config');

class elves{

    constructor() {
    }

    async valid(cookie){
        return new Promise((resolve, reject) => {
            let options = {
                url: 'https://www.sellersprite.com/v2/keyword/google-trends.json?gprop=&intervalYear=5&gv=false&monthly=false&station=COM&keyword=face%20mask',
                method: 'GET',
                // proxy: `http://${config.PROXY.HOST}:${config.PROXY.PORT}`,
                headers: {
                    "cookie": cookie,
                    // "accept": 'application/json, text/javascript, */*; q=0.01'
                }
            };

            request(options, async (err, response, body) => {
                if (err) {
                    console.log(`sellersprite cookie invalid. error:${JSON.stringify(err)}`);
                    if (err.code === 'ECONNRESET') {
                        resolve(true);
                    } else {
                        resolve(false);
                    }
                } else {
                    body = JSON.parse(body);
                    if(Object.keys(body).length === 0 || body.code === 'ERR_ENND_RE_AUTHORIZED'){
                        console.log(`sellersprite cookie invalid. Body:${JSON.stringify(body)}`);
                        resolve(false);
                    }else{
                        console.log(`sellersprite cookie valid passed...`);
                        resolve(true);
                    }
                }
            })
        })
    }

}
module.exports = elves;
